<template>
  <div class="wp-pwd">
    <el-form
      :model="loginAccount"
      ref="settingPwdForm"
      label-position="right"
      :rules="rules"
      status-icon
      label-width="100px"
    >
      <el-form-item label="原密码" prop="oldPassword">
        <el-input
          class="size"
          type="password"
          v-model.trim="loginAccount.oldPassword"
          placeholder="请输入原来密码"
          clearable
          show-password
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="password">
        <el-input
          class="size"
          type="password"
          v-model.trim="loginAccount.password"
          placeholder="请输入新密码"
          clearable
          show-password
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPassword">
        <el-input
          class="size"
          type="password"
          v-model.trim="loginAccount.checkPassword"
          placeholder="确认新密码"
          clearable
          show-password
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
type="success" class="size" @click.native="save"
          >保存</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { updateLoginPasswordAPI } from '@/api/user/userCenterAPI'
import { userLogout } from '@/utils/auth'
export default {
  name: 'EditUserPasswd',
  components: {},
  data () {
    const pwdReg = /^(?=.*[a-zA-Z])(?=.*\d).{6,15}$/
    const validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        if (!pwdReg.test(value)) {
          callback(new Error('密码由6-20位字母、数字组成'))
        } else {
          if (this.loginAccount.checkPassword !== '') {
            this.$refs.settingPwdForm.validateField('checkPassword')
          }
          callback()
        }
      }
    }
    const validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.loginAccount.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      loginAccount: {
        oldPassword: '',
        password: '',
        checkPassword: ''
      },
      rules: {
        oldPassword: [
          { required: true, message: '请输入原来密码', trigger: 'blur' }
        ],
        password: [
          { required: true, validator: validatePass, trigger: 'blur' }
        ],
        checkPassword: [
          { required: true, validator: validatePass2, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    save () {
      this.$refs.settingPwdForm.validate((valid) => {
        if (valid) {
          const params = {}
          params.originalPwd = this.loginAccount.oldPassword
          params.newPwd = this.loginAccount.password
          updateLoginPasswordAPI(params).then(res => {
            if (res.code === 0) {
              this.$msgBox.delayeOff('提示', '修改成功！需要重新登录', () => {
                this.$emit('afterSave')
                userLogout()
              }, 3)
            }
          }, 3)
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.wp-pwd {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 80%;
}
.size {
  width: 280px;
}
</style>
